package com.cloud.controller.wxsubscribe;

import com.cloud.commons.utils.message.Result;
import com.cloud.commons.utils.message.ResultUtil;
import com.cloud.commons.utils.message.MessageDescription;
import com.cloud.commons.utils.message.MessageCode;
import com.cloud.model.view.wxsubscribe.WxSubscribeView;
import com.cloud.service.wxsubscribe.WxSubscribeService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * <b><code>WxSubscribeController</code></b>
 * <p/>
 * WxSubscribe的具体实现的Api类，提供统一的api调用
 * <p/>
 * <b>Creation Time:</b> 2020-01-11 17:45:41.
 *
 * @author ${model.author}
 * @version 1.0.0
 * @since cloud-spring-boot-be 1.0.0
 */
@Api(value = "[Cloud Source]订阅消息接口",tags = "[Cloud Source]订阅消息接口",description = "")
@RestController
@RequestMapping(value = "/api")
public class WxSubscribeController {

    /** The Constant LOG. */
    private static final Logger LOG = LoggerFactory
            .getLogger(WxSubscribeController.class);

    /** The service. */
    @Autowired
    private WxSubscribeService wxSubscribeService;

    @ApiOperation(value = "[Cloud Source]创建订阅消息", notes = "创建一个订阅消息")
    @ApiResponses(value = {
            @ApiResponse(code = 201, message = "successful operation", responseHeaders = @ResponseHeader(name = "location", description = "URL of new created resource", response = String.class) ),
            @ApiResponse(code = 409, message = "conflict"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxsubscribes", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> createWxSubscribe(
            @ApiParam(value = "订阅消息", required = true) @RequestBody WxSubscribeView wxSubscribeView) {
        try {
            // 保存实体
            WxSubscribeView wxSubscribeViewReturn = wxSubscribeService.saveEntity(wxSubscribeView);

            // 封装返回信息
             return ResultUtil.success(MessageCode.SUCCESS_CODE_200, MessageDescription.OPERATION_INSERT_SUCCESS,wxSubscribeViewReturn);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]删除订阅消息", notes = "通过id删除订阅消息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxsubscribes/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> deleteWxSubscribe(
            @ApiParam(value = "订阅消息id", required = true) @PathVariable(value = "id") Long id) {
        try {
            wxSubscribeService.deleteEntity(id);
             return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_DELETE_SUCCESS,null);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]删除订阅消息", notes = "批量删除订阅消息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxsubscribes", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> deleteWxSubscribes(
            @ApiParam(value = "订阅消息ids，样例 - 1,2,3", required = true) @RequestParam(value = "condition", required = true) String condition) {
        try {
            wxSubscribeService.deleteEntities(condition);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_DELETE_SUCCESS,null);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]更新订阅消息", notes = "更新订阅消息信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "successful"),
            @ApiResponse(code = 404, message = "not found"),
            @ApiResponse(code = 409, message = "conflict"),
            @ApiResponse(code = 500, message = "internal Server Error") })
    @RequestMapping(value = "/v1/wxsubscribes/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> updateWxSubscribes(
            @ApiParam(value = "订阅消息id", required = true) @PathVariable(value = "id") Long id,
            @ApiParam(value = "订阅消息信息", required = true) @RequestBody WxSubscribeView wxSubscribeView) {
        try {
            wxSubscribeView.setId(id);
            wxSubscribeService.updateEntity(wxSubscribeView);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_UPDATE_SUCCESS, wxSubscribeView);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]获取单个订阅消息", notes = "通过id获取订阅消息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxsubscribes/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> getWxSubscribe(
            @ApiParam(value = "订阅消息id", required = true) @PathVariable(value = "id") Long id) {
        try {
            final WxSubscribeView wxSubscribeView = wxSubscribeService.getEntity(id);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_QUERY_SUCCESS, wxSubscribeView);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

}
